/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hagopa.games.moneypool.msgs.ContentTypes.service;

/**
 *
 * @author Igor
 */
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import org.hagopa.games.moneypool.msgs.IMsgContent;

public class BServMsg implements IMsgContent{

	private int total_size = 0;
        private int type = 0;
	private int ActiveClientId = 0;
        byte []all = null;
        private String msg2send;



    @Override
	public byte[] marshallToByte() {
            byte [] bytes = new byte[1024];
            ByteBuffer dataArray = ByteBuffer.allocate(1024);
            dataArray.putInt(this.type);
            dataArray.putInt(this.ActiveClientId);
            System.out.println("Array len: " + this.total_size);
            try{
                
                dataArray.put(this.msg2send.getBytes());
                }catch (NullPointerException e1) {
			// TODO Auto-generated catch block
			System.out.println("Failed to set bytes array");
			e1.printStackTrace();
		}
            dataArray.rewind();
            dataArray.get(bytes, 0, bytes.length);
            return bytes;
        }
        
    @Override
        public boolean unmarshallFromByte(byte[] content) {
		this.all = content;


		return true;
	}
        public void setType(int type)
        {
            this.type = type;
        }
        public void setMsg(String msg)
        {
            this.msg2send = msg;
        }
    @Override
        public int getSize()
        {
		return this.total_size;
	}

	public int getSenderId() {
		return ActiveClientId;
	}
        
	public void setSenderId(int senderId) {
		this.ActiveClientId = senderId;
	}


	public int getTotal_size() {
		return total_size;
	}


	public void setTotal_size(int total_size) {
		this.total_size = total_size + 4 + 4 ;
	}


	public void setAll(byte[] all) {
		this.all = all;
               // this.total_size = this.all.length;
	}

    @Override
        public String getConcatContent() {
            String outstr = null;
            outstr = String.valueOf(this.type)+ String.valueOf(this.ActiveClientId) + this.msg2send ;
            return outstr;
    }
}
